<?php

namespace app\manystore\controller\salescenter;

use app\common\controller\ManystoreBase;
use app\common\library\Ems;
use app\common\library\Sms;
use fast\Random;
use think\Config;
use think\Validate;

use think\Db;
use app\manystore\Model\UserModel;
use app\manystore\Model\CampusClewConsultantModel;
use app\manystore\Model\CampusFollowModel;
use app\manystore\Model\CampusAuditionRecordModel;
use app\manystore\Model\CampusCourseOrderModel;
use app\manystore\Model\CampusCourseOrderSublevelModel;

/**
 * 警告接口
 */
class EarlyWarning extends ManystoreBase
{
    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';

    public function _initialize()
    {
        parent::_initialize();
    }

    /**
     * 获取顾问列表
     *
     * @ApiTitle    获取顾问列表
     * @ApiMethod   (GET)

     * @ApiParams (name="search_key", type="string", required=false, description="条件")
     * @ApiSector   顾问管理
     * @ApiReturn   ({
        'code' : '1',
        'msg' : '获取数据成功',
        'time' : '1657378506',
        'data' : {
		  "code": 1,
		  "msg": "获取数据成功",
		  "time": "1657876279",
		  "data": {
		    "count": 1,
		    "list": [
		      {
		        "id": 1,
		        "avatar": "/uploads/20220219/3989ac48df6167ae81b3a29d3541ef94.jpg",//头像相对路径
		        "truename": "小天",//姓名
		        "totalclew_num": 0,//'总线索数
		      }
		    ]
		  }
		}
     })
     */
    public function getConsultantList()
    {
    	$param = $this->request->param();

    	$list = CampusClewConsultantModel::field('id,avatar,truename,totalclew_num')->where(function($query) use ($param){
            if(!empty($param['search_key']))
            {
            	$map = [
            		'truename' => ['like',"%{$param['search_key']}%"],
            		'mobile' => ['like',"%{$param['search_key']}%"],
            	];

                $query->whereOr($map);
            }
        })->select();

        $data = ['count' => count($list),'list' => $list];

        $this->success('获取数据成功','',$data);
    }

    /**
     * 获取顾问详情
     *
     * @ApiTitle    获取顾问详情
     * @ApiMethod   (GET)

     * @ApiParams (name="id", type="integer", required=true, description="顾问ID")
     * @ApiSector   顾问管理
     * @ApiReturn   ({
		  "code": 1,
		  "msg": "获取数据成功",
		  "time": "1657876279",
		  "data": {
				"id": 1,
				"avatar": "/uploads/20220219/3989ac48df6167ae81b3a29d3541ef94.jpg",//头像相对路径
				"truename": "小天",//姓名
				"mobile": "186563656",//手机号
				"gender": 1,//性别:1=男,2=女
				"totalclew_num": 0,//'总线索数
				"lm_audition_peoplenum": 0,//上个月试听人数
				"cm_audition_peoplenum": 0,//本月试听人数
				"lm_dealdone_peoplenum": 0,//上月成交人数
				"cm_dealdone_peoplenum": 0,//本月成交人数
				"lm_sale_amount": "0.00",//上月销售额【元】
				"cm_sale_amount": "0.00",//本月销售额【元】
				"updatetime": null,//更新时间
				"createtime": 1657876279//创建时间
		  }
     * })
     */
    public function getConsultantInfo()
    {
    	$id = intval($this->request->param('id'));

    	if(empty($id))
    	{
    		$this->error('未查询到顾问信息!');
    	}

    	$info = CampusClewConsultantModel::where(['id' => $id])->find();

    	if(empty($info))
    	{
    		$this->error('未查询到顾问信息!');
    	}

        $this->success('获取数据成功','',$info);
    }

    /**
     * 获取顾问所属学员列表
     *
     * @ApiTitle    获取顾问所属学员列表
     * @ApiMethod   (GET)

     * @ApiParams (name="id", type="integer", required=true, description="顾问ID")
     * @ApiSector   顾问管理
     * @ApiReturn   ({
		  "code": 1,
		  "msg": "获取数据成功",
		  "time": "1657876279",
		  "data": {
				"id": 1,
				"avatar": "/uploads/20220219/3989ac48df6167ae81b3a29d3541ef94.jpg",//头像相对路径
				"truename": "小天",//姓名
				"mobile": "186563656",//手机号
				"gender": 1,//性别:1=男,2=女
				"totalclew_num": 0,//'总线索数
				"lm_audition_peoplenum": 0,//上个月试听人数
				"cm_audition_peoplenum": 0,//本月试听人数
				"lm_dealdone_peoplenum": 0,//上月成交人数
				"cm_dealdone_peoplenum": 0,//本月成交人数
				"lm_sale_amount": "0.00",//上月销售额【元】
				"cm_sale_amount": "0.00",//本月销售额【元】
				"updatetime": null,//更新时间
				"createtime": 1657876279//创建时间
		  }
     * })
     */
    public function getStudentList()
    {
    	$id = intval($this->request->param('id'));

    	if(empty($id))
    	{
    		$this->error('顾问不存在!');
    	}

    	$info = CampusClewConsultantModel::where(['id' => $id])->find();

    	if(empty($info))
    	{
    		$this->error('顾问不存在!');
    	}

    	$list = UserModel::field('truename,mobile,gender')->where(['course_consultant' => $info['id']])->select();

    	$data = ['count' => count($list),'list' => $list];

        $this->success('获取数据成功','',$data);
    }

    /**
     * 获取指定顾问跟进的学员列表
     *
     * @ApiTitle    获取指定顾问跟进的学员列表
     * @ApiMethod   (GET)

     * @ApiParams (name="id", type="integer", required=true, description="顾问ID")
     * @ApiSector   顾问管理
     * @ApiReturn   {
		  "code": 1,
		  "msg": "获取数据成功",
		  "time": "1657886737",
		  "data": {
		    "count": 1,
		    "list": [
		      {
		        "truename": "张小福",//学员姓名
		        "follow_way": "电话",//学院电话
		        "consultant": "小天",//顾问姓名
		        "task_status": 1,//任务状态:0=未开始,1=已取消,2=已逾期,3=已完成
		        "complete_time": null//完成时间【时间戳】
		      }
		    ]
		  }
		}
     */
    public function getFollowUpList()
    {
    	$id = intval($this->request->param('id'));

    	if(empty($id))
    	{
    		$this->error('跟进人不存在!');
    	}

    	$info = CampusClewConsultantModel::where(['id' => $id])->find();

    	if(empty($info))
    	{
    		$this->error('跟进人不存在!');
    	}

    	$list = CampusFollowModel::field('b.truename,d.name as follow_way,c.truename as consultant,a.task_status,a.complete_time,b.course_consultant')
    	->alias('a')
    	->join('ss_user b', 'a.clew_id = b.id','left')
    	->join('ss_campus_clew_consultant c', 'b.course_consultant = c.id','left')
    	->join('ss_type d', 'a.follow_way = d.id','left')
    	->where(['a.follow_uid' => $info['id']])->select();

    	$data = ['count' => count($list),'list' => $list];

        $this->success('获取数据成功','',$data);
    }

    /**
     * 获取指定顾问所属的学员试听记录列表
     *
     * @ApiTitle    获取指定顾问跟进的学员试听记录列表
     * @ApiMethod   (GET)

     * @ApiParams (name="id", type="integer", required=true, description="顾问ID")
     * @ApiSector   顾问管理
     * @ApiReturn   {
		  "code": 1,
		  "msg": "获取数据成功",
		  "time": "1657886737",
		  "data": {
		    "count": 1,
		    "list": [
		      {
		        "truename": "张晓曦",//学员姓名
		        "course_name": "测试1",//课程名称
		        "consultant": "小天",//顾问姓名
		        "createtime": 1657893779,//时间【时间戳】
		        "status": 1//状态:1=已预约,2=已体验,3=已取消,4=未到课
		      }
		    ]
		  }
		}
     */
    public function getFollowUpOfAuditionRecordList()
    {
    	$id = intval($this->request->param('id'));

    	if(empty($id))
    	{
    		$this->error('课程顾问不存在!');
    	}

    	$info = CampusClewConsultantModel::where(['id' => $id])->find();

    	if(empty($info))
    	{
    		$this->error('课程顾问不存在!');
    	}

    	$list = CampusAuditionRecordModel::field('b.truename,d.name as course_name,c.truename as consultant,a.createtime,a.status')
    	->alias('a')
    	->join('ss_user b', 'a.clew_id = b.id','left')
    	->join('ss_campus_clew_consultant c', 'b.course_consultant = c.id','left')
    	->join('ss_course d', 'a.course = d.id','left')
    	->where(['b.course_consultant' => $info['id']])->select();

    	$data = ['count' => count($list),'list' => $list];

        $this->success('获取数据成功','',$data);
    }

    /**
     * 获取指定顾问所属的学员报课订单表
     *
     * @ApiTitle    获取指定顾问所属的学员报课订单表
     * @ApiMethod   (GET)

     * @ApiParams (name="id", type="integer", required=true, description="顾问ID")
     * @ApiSector   顾问管理
     * @ApiReturn   {
		  "code": 1,
		  "msg": "获取数据成功",
		  "time": "1657886737",
		  "data": {
		    "count": 1,
		    "list": [
		      {
		        "no": "20220623154315320769",//订单号
		        "truename": "张晓曦",//学员姓名
		        "mobile": "13333333333",//学员手机号
		        "createtime": "06月23日 15:43",//订单创建时间
		        "good_name": "课程-绘画1",//商品名称
		        "course": [
		          {
		            "name": "素描1",//包含的课程名称
		            "price": "3001.00",//单价【每课时、日、月的价格】
		            "type": "课时"//类型【课时、日、月】
		          }
		        ]
		      }
		    ]
		  }
		}
     */
    public function getStudentOfCourseOrderList()
    {
    	$id = intval($this->request->param('id'));

    	if(empty($id))
    	{
    		$this->error('课程顾问不存在!');
    	}

    	$info = CampusClewConsultantModel::where(['id' => $id])->find();

    	if(empty($info))
    	{
    		$this->error('课程顾问不存在!');
    	}

    	$list = CampusCourseOrderSublevelModel::field('a.no,b.truename,b.mobile,d.createtime,c.name as good_name,a.m_type,c.hour,c.monthly,c.day')
    	->alias('a')
    	->join('ss_user b', 'a.user_id = b.id','left')
    	->join('ss_campus_course_mall_goods c', 'c.id = a.gs_id','left')
    	->join('ss_campus_course_order d', 'd.no = a.no','left')
    	->where(['b.course_consultant' => $info['id']])->select();

    	$types = ['hour' => '课时','monthly' => '月','day' => '天'];

    	foreach ($list as & $value)
    	{
    		$type = $value['m_type'];

    		$course = [];

    		if(isset($value[$type]))
    		{
    			$type_goods = unserialize($value[$type]);

    			foreach ($type_goods as $v)
    			{
    				$course[] = [
    					'name' => $v['name'],
    					'price' => sprintf("%01.2f", $v['price']),
    					'type' => $types[$type],
    				];
    			}
    		}

    		$value['course'] = $course;
    		$value['createtime'] = date('m月d日 H:i',$value['createtime']);

    		unset($value['m_type']);
    		unset($value['hour']);
    		unset($value['monthly']);
    		unset($value['day']);
    	}

    	$data = ['count' => count($list),'list' => $list];

        $this->success('获取数据成功','',$data);
    }

    
}